﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Microsoft.Win32;
namespace Congducsan
{
    public partial class FrmTchanTn : Form
    {
        public FrmTchanTn()
        {
            InitializeComponent();
        }

        private void tabControl2_Click(object sender, EventArgs e)
        {
            if (tabControl2.SelectedTab==tabControl2.TabPages[0])
            {
                pictureBox1.Image = Congducsan.Properties.Resources.Tcsand;
            }
            else
            {
                pictureBox1.Image = Congducsan.Properties.Resources.Tcsans;
            }
        }

        private void FrmTchanTn_FormClosing(object sender, FormClosingEventArgs e)
        {
            DradApplication.DradApp.FormMain.Show();
        }

        private void FrmTchanTn_Resize(object sender, EventArgs e)
        {
            this.Size = new Size(653, 535);
        }

        private void CmdOk_Click(object sender, EventArgs e)
        {
            PrTchanTn prTchanTn = new PrTchanTn();
            prTchanTn.Show();
            this.Hide();
        }

        private void FrmTchanTn_Load(object sender, EventArgs e)
        {
            loadDataGridView();
            loadSettingReg();
        }

        private void cmdThietlap_Click(object sender, EventArgs e)
        {
            FrmTuychon frmTuychon = new FrmTuychon();
            frmTuychon.Show();
        }
        
        //hàm nhận giá trị đưa vào datagridview
        private void SanTn1(int col, int row, string Value)
        {
            col = col - 1;
            row = row - 1;
            dgvSanTieuNang.Rows[row].Cells[col].Value = Value;
        }

        //hàm lấy giá trị từ datagridview
        private string SanTn2(int col, int row)
        {
            col = col - 1;
            row = row - 1;
            string Value = dgvSanTieuNang.Rows[row].Cells[col].Value.ToString();
            return Value;
        }

        // Lưu settings vào registry
        private void saveSettingReg()
        {
            RegistryKey regKey = Registry.CurrentUser;
            regKey = regKey.CreateSubKey("Software\\HaithanhSoft\\Drad2012\\Congducsan");
            string Values = "";
            //Tuong chan
            Values = txtCaotuongchanH.Text;//0
            Values = Values +"\t"+ txtBotriXamu.Text;//1
            Values = Values +"\t"+ txtXamub1.Text;//2
            Values = Values +"\t"+ txtXamuh1.Text;//3
            Values = Values +"\t"+ txtVatlungv1.Text;//4
            Values = Values +"\t"+ txtVatlungv2.Text;//5
            Values = Values +"\t"+ txtDaymongtruocH1.Text;//6
            Values = Values +"\t"+ txtDaymongsauH2.Text;//7
            Values = Values +"\t"+ txtMomongtruocb2.Text;//8
            Values = Values +"\t"+ txtMomongsaub3.Text;//9
            Values = Values +"\t"+ txtRongmongb.Text;//10
            Values = Values +"\t"+ txtNghiengtuongchani.Text;//11
            Values = Values +"\t"+ txtDaituongchanL.Text;//12
            Values = Values +"\t"+ txtKcOngcongd1.Text;//13
            Values = Values +"\t"+ txtKcBaccapd2.Text;//14
            //San tieu nang
            Values = Values +"\t"+ txtSophandoan.Text;//15
            Values = Values +"\t"+ SanTn2(2, 1);//16
            Values = Values +"\t"+ SanTn2(2, 2);//17
            Values = Values +"\t"+ SanTn2(2, 3);//18
            Values = Values +"\t"+ SanTn2(2, 4);//19
            Values = Values +"\t"+ SanTn2(2, 5);//20
            Values = Values +"\t"+ SanTn2(2, 6);//21
            Values = Values +"\t"+ SanTn2(2, 7);//22
            Values = Values +"\t"+ SanTn2(2, 8);//23
            Values = Values +"\t"+ SanTn2(2, 9);//24
            Values = Values +"\t"+ SanTn2(2, 10);//25
            Values = Values +"\t"+ SanTn2(2, 11);//26
            Values = Values +"\t"+ SanTn2(2, 12);//27
            Values = Values +"\t"+ SanTn2(2, 13);//28
            Values = Values +"\t"+ SanTn2(2, 14);//29
            Values = Values +"\t"+ SanTn2(2, 15);//30
            Values = Values +"\t"+ SanTn2(2, 16);//31
            Values = Values +"\t"+ SanTn2(2, 17);//32
            Values = Values +"\t"+ SanTn2(2, 18);//33
            Values = Values +"\t"+ txtRongroda.Text;//34
            Values = Values +"\t"+ txtCaoroda.Text;//35
            Values = Values +"\t"+ txtDairoda.Text;//36
            Values = Values +"\t"+ txtLopdem.Text;//37
            regKey.SetValue("TuongchanTn", Values);
            regKey.Close();
            
        }

        // Load settings từ registry vào form
        private void loadSettingReg()
        {
            RegistryKey regKey = Registry.CurrentUser;
            RegistryKey testKey = regKey.OpenSubKey("Software\\HaithanhSoft\\Drad2012\\Congducsan");
            if (testKey != null)
            {
                regKey = regKey.CreateSubKey("Software\\HaithanhSoft\\Drad2012\\Congducsan");
                string str = regKey.GetValue("TuongchanTn").ToString();
                string[] STR = str.Split('\t');
                int i = 0;
                //Tuong chan
                txtCaotuongchanH.Text=STR[i++];//0
                txtBotriXamu.Text = STR[i++];//1
                txtXamub1.Text = STR[i++];//2
                txtXamuh1.Text = STR[i++];//3
                txtVatlungv1.Text = STR[i++];//4
                txtVatlungv2.Text = STR[i++];//5
                txtDaymongtruocH1.Text = STR[i++];//6
                txtDaymongsauH2.Text = STR[i++];//7
                txtMomongtruocb2.Text = STR[i++];//8
                txtMomongsaub3.Text = STR[i++];//9
                txtRongmongb.Text = STR[i++];//10
                txtNghiengtuongchani.Text = STR[i++];//11
                txtDaituongchanL.Text = STR[i++];//12
                txtKcOngcongd1.Text = STR[i++];//13
                txtKcBaccapd2.Text = STR[i++];//14
                //San tieu nang
                txtSophandoan.Text = STR[i++];//15
                SanTn1(2, 1, STR[i++]);//16
                SanTn1(2, 2, STR[i++]);//17
                SanTn1(2, 3, STR[i++]);//18
                SanTn1(2, 4, STR[i++]);//19
                SanTn1(2, 5, STR[i++]);//20
                SanTn1(2, 6, STR[i++]);//21
                SanTn1(2, 7, STR[i++]);//22
                SanTn1(2, 8, STR[i++]);//23
                SanTn1(2, 9, STR[i++]);//24
                SanTn1(2, 10, STR[i++]);//25
                SanTn1(2, 11, STR[i++]);//26
                SanTn1(2, 12, STR[i++]);//27
                SanTn1(2, 13, STR[i++]);//28
                SanTn1(2, 14, STR[i++]);//29
                SanTn1(2, 15, STR[i++]);//30
                SanTn1(2, 16, STR[i++]);//31
                SanTn1(2, 17, STR[i++]);//32
                SanTn1(2, 18, STR[i++]);//33
                txtRongroda.Text = STR[i++];//34
                txtCaoroda.Text = STR[i++];//35
                txtDairoda.Text = STR[i++];//36
                txtLopdem.Text = STR[i++];//37
                regKey.Close();
            }
        }

        // Save setting vào file
        private void saveSettingFile()
        {
            SaveFileDialog svDialog = new SaveFileDialog();
            svDialog.Filter = "Data files (*.tcn)|*.tcn";
            svDialog.InitialDirectory = "C://";
            svDialog.RestoreDirectory = true;
            string filename = "";
            if (svDialog.ShowDialog() == DialogResult.OK)
            {
                filename = svDialog.FileName;
                string Str;
                    //Tuong chan
                Str = "------TUONG CHAN------" +"\r\n";
                Str = Str + "Cao tuong chan:" +"\t"+ txtCaotuongchanH .Text+"\r\n";
                Str = Str + "Bo tri xa mu:" +"\t"+ txtBotriXamu .Text+"\r\n";
                Str = Str + "Rong xa mu:" +"\t"+ txtXamub1 .Text+"\r\n";
                Str = Str + "Cao xa mu:" +"\t"+ txtXamuh1 .Text+"\r\n";
                Str = Str + "Cao vat lung:" +"\t"+ txtVatlungv1 .Text+"\r\n";
                Str = Str + "Rong vat lung:" +"\t"+ txtVatlungv2 .Text+"\r\n";
                Str = Str + "Day mong truoc:" +"\t"+ txtDaymongtruocH1 .Text+"\r\n";
                Str = Str + "Day mong sau:" +"\t"+ txtDaymongsauH2 .Text+"\r\n";
                Str = Str + "Nghieng lung tuong chan:" +"\t"+ txtMomongtruocb2 .Text+"\r\n";
                Str = Str + "Nghieng lung tuong chan:" +"\t"+ txtMomongsaub3 .Text+"\r\n";
                Str = Str + "Rong mong tuong chan:" +"\t"+ txtRongmongb .Text+"\r\n";
                Str = Str + "Nghieng lung tuong chan:" +"\t"+ txtNghiengtuongchani .Text+"\r\n";
                Str = Str + "Dai tuong chan:" +"\t"+ txtDaituongchanL .Text+"\r\n";
                Str = Str + "Khoang cach dinh cap - cua ra:" +"\t"+ txtKcOngcongd1 .Text+"\r\n";
                Str = Str + "Khoang cach dinh mong - dinh cap:" +"\t"+ txtKcBaccapd2 .Text+"\r\n";
                Str = Str + "Lop dem mong:" +"\t"+ txtLopdem .Text+"\r\n";
                //San tieu nang
                Str = Str + "----SAN TIEU NANG------" +"\r\n";
                Str = Str + "So phan doan:" +"\t"+ txtSophandoan .Text+"\r\n";
                Str = Str + "Day tuong T:" +"\t"+ SanTn2(2, 1)+"\r\n";
                Str = Str + "Cao tuong H:" +"\t"+ SanTn2(2, 2)+"\r\n";
                Str = Str + "Day san h1:" +"\t"+ SanTn2(2, 3)+"\r\n";
                Str = Str + "Day mong be h2:" +"\t"+ SanTn2(2, 4) +"\r\n";
                Str = Str + "Day san h3:" +"\t"+ SanTn2(2, 5) +"\r\n";
                Str = Str + "Sau chan khay H1:" +"\t"+ SanTn2(2, 6) +"\r\n";
                Str = Str + "Sau be H2:" +"\t"+ SanTn2(2, 7) +"\r\n";
                Str = Str + "Sau be H3:" +"\t"+ SanTn2(2, 8) +"\r\n";
                Str = Str + "Sau chan khay H4:" +"\t"+ SanTn2(2, 9) +"\r\n";
                Str = Str + "Dai san L:" +"\t"+ SanTn2(2, 10) +"\r\n";
                Str = Str + "Dai be L1:" +"\t"+ SanTn2(2, 10) +"\r\n";
                Str = Str + "Dai san L2:" +"\t"+ SanTn2(2, 12) +"\r\n";
                Str = Str + "Day chan khay b:" +"\t"+ SanTn2(2, 13) +"\r\n";
                Str = Str + "Day chan khay b1:" +"\t"+ SanTn2(2, 14) +"\r\n";
                Str = Str + "Day thanh be b2:" +"\t"+ SanTn2(2, 15) +"\r\n";
                Str = Str + "Vat thanh be a" +"\t"+ SanTn2(2, 16) +"\r\n";
                Str = Str + "Rong go d" +"\t"+ SanTn2(2, 17) +"\r\n";
                Str = Str + "Cach khoang go d1" +"\t"+ SanTn2(2, 18) +"\r\n";
                Str = Str + "Rong san tieu nang" +"\t"+ txtRongsantieunang .Text+"\r\n";
                Str = Str + "Bo tri ro da" +"\t"+ ChcRd.Checked.ToString()+"\r\n";
                Str = Str + "Cao ro da" +"\t"+ txtCaoroda .Text+"\r\n";
                Str = Str + "Rong ro da" +"\t"+ txtRongroda .Text+"\r\n";
                Str = Str + "Dai ro da" +"\t"+ txtDairoda .Text+"\r\n";
                Str = Str + "So hang ro da" + "\t" + txtHangRoda.Text;
                //Ghi file dữ liệu
                FileStream fs = new FileStream(filename, FileMode.OpenOrCreate);
                StreamWriter sw = new StreamWriter(fs);
                sw.Write(Str);
                sw.Close();
                fs.Close();
                MessageBox.Show("OK", "Thông báo");

            }
        }
         // load setting từ file vào form
        private void loadSettingFile()
        {
            OpenFileDialog openDialog = new OpenFileDialog();
            openDialog.Filter = "Data files (*.tcn)|*.tcn";
            openDialog.InitialDirectory = @"C:\";
            openDialog.FilterIndex = 2;
            openDialog.RestoreDirectory = true;
            openDialog.Multiselect = false;
            string filename;
            if (openDialog.ShowDialog() == DialogResult.OK)
            {
                filename = openDialog.FileName;
                FileStream fs = new FileStream(filename, FileMode.Open);
                StreamReader sr = new StreamReader(fs);
                string str = sr.ReadToEnd();
                fs.Close();
                fs.Close();
                string[] Values = str.Split('\n');
                int i = 1;
                    //Tuong chan
                txtCaotuongchanH .Text=Values[i].Split('\t')[1]; i++;
                txtBotriXamu .Text=Values[i].Split('\t')[1]; i++;
                txtXamub1 .Text=Values[i].Split('\t')[1]; i++;
                txtXamuh1 .Text=Values[i].Split('\t')[1]; i++;
                txtVatlungv1 .Text=Values[i].Split('\t')[1]; i++;
                txtVatlungv2 .Text=Values[i].Split('\t')[1]; i++;
                txtDaymongtruocH1 .Text=Values[i].Split('\t')[1]; i++;
                txtDaymongsauH2 .Text=Values[i].Split('\t')[1]; i++;
                txtMomongtruocb2 .Text=Values[i].Split('\t')[1]; i++;
                txtMomongsaub3 .Text=Values[i].Split('\t')[1]; i++;
                txtRongmongb .Text=Values[i].Split('\t')[1]; i++;
                txtNghiengtuongchani .Text=Values[i].Split('\t')[1]; i++;
                txtDaituongchanL .Text=Values[i].Split('\t')[1]; i++;
                txtKcOngcongd1 .Text=Values[i].Split('\t')[1]; i++;
                txtKcBaccapd2 .Text=Values[i].Split('\t')[1]; i++;
                txtLopdem .Text=Values[i].Split('\t')[1]; i++;
                //San tieu nang
                i = i + 1;
                txtSophandoan.Text = Values[i].Split('\t')[1]; i++;
                SanTn1(2, 1, Values[i].Split('\t')[1]); i++;
                SanTn1(2, 2, Values[i].Split('\t')[1]) ; i++;
                SanTn1(2, 3, Values[i].Split('\t')[1]); i++;
                SanTn1(2, 4, Values[i].Split('\t')[1]) ; i++;
                SanTn1(2, 5, Values[i].Split('\t')[1]) ; i++;
                SanTn1(2, 6, Values[i].Split('\t')[1]); i++;
                SanTn1(2, 7, Values[i].Split('\t')[1]) ; i++;
                SanTn1(2, 8, Values[i].Split('\t')[1]) ; i++;
                SanTn1(2, 9, Values[i].Split('\t')[1]) ; i++;
                SanTn1(2, 10, Values[i].Split('\t')[1]) ; i++;
                SanTn1(2, 11, Values[i].Split('\t')[1]); i++;
                SanTn1(2, 12, Values[i].Split('\t')[1]) ; i++;
                SanTn1(2, 13, Values[i].Split('\t')[1]) ; i++;
                SanTn1(2, 14, Values[i].Split('\t')[1]) ; i++;
                SanTn1(2, 15, Values[i].Split('\t')[1]); i++;
                SanTn1(2, 16, Values[i].Split('\t')[1]) ; i++;
                SanTn1(2, 17, Values[i].Split('\t')[1]) ; i++;
                SanTn1(2, 18, Values[i].Split('\t')[1]); i++;
                txtRongsantieunang .Text=Values[i].Split('\t')[1]; i++;
                ChcRd.Checked=bool.Parse(Values[i].Split('\t')[1]); i++;
                txtCaoroda .Text=Values[i].Split('\t')[1]; i++;
                txtRongroda .Text=Values[i].Split('\t')[1]; i++;
                txtDairoda .Text=Values[i].Split('\t')[1]; i++;
                txtHangRoda .Text=Values[i].Split('\t')[1]; i++;
                MessageBox.Show("OK", "Thông báo");
            }
        }
        //Hiện thị dữ liệu lên dgv
        private void loadDataGridView()
        {
            dgvSanTieuNang.Rows.Add("Dày tường T");
            dgvSanTieuNang.Rows.Add("Cao tường h");
            dgvSanTieuNang.Rows.Add("Dày sân h1");
            dgvSanTieuNang.Rows.Add("Dày móng bể h2");
            dgvSanTieuNang.Rows.Add("Dày sân h3");
            dgvSanTieuNang.Rows.Add("Sâu chân khay H1");
            dgvSanTieuNang.Rows.Add("Sâu bê H2");
            dgvSanTieuNang.Rows.Add("Sâu bê H3");
            dgvSanTieuNang.Rows.Add("Sâu chân khay H4");
            dgvSanTieuNang.Rows.Add("Dài sân L");
            dgvSanTieuNang.Rows.Add("Dài bể L1");
            dgvSanTieuNang.Rows.Add("Dài sân L2");
            dgvSanTieuNang.Rows.Add("Dày chân khay b");
            dgvSanTieuNang.Rows.Add("Dày chân khay b1");
            dgvSanTieuNang.Rows.Add("Dày thành bể b2");
            dgvSanTieuNang.Rows.Add("Vát thành bể a");
            dgvSanTieuNang.Rows.Add("Rộng gờ d");
            dgvSanTieuNang.Rows.Add("Cách khoảng gờ d1");
        }

        private void SaveData_Click(object sender, EventArgs e)
        {
            saveSettingFile();
        }

        private void LoadData_Click(object sender, EventArgs e)
        {
            loadSettingFile();
        }

        private void Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FrmTchanTn_FormClosed(object sender, FormClosedEventArgs e)
        {
            saveSettingReg();
        }
    }
}
